package com.ruoyi.system.service.impl.fxImpl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FxDistributionSetting;
import com.ruoyi.system.domain.fx.FxDistribution;
import com.ruoyi.system.domain.fx.FxOrder;
import com.ruoyi.system.domain.fx.FxUser;
import com.ruoyi.system.domain.vo.StatisticsVo;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.mapper.fx.FxDistributionMapper;
import com.ruoyi.system.mapper.fx.FxUserMapper;
import com.ruoyi.system.service.fx.IFxDistributionService;
import com.ruoyi.system.service.fx.IFxDistributionSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Random;
import java.util.Scanner;


/**
 * 分销提成Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class FxDistributionServiceImpl implements IFxDistributionService
{
    @Autowired
    private FxDistributionMapper fxDistributionMapper;
    @Autowired
    private FxUserMapper fxUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private IFxDistributionSettingService fxDistributionSettingService;

    /**
     * 查询分销提成
     * 
     * @param id 分销提成主键
     * @return 分销提成
     */
    @Override
    public FxDistribution selectFxDistributionById(Long id)
    {
        return fxDistributionMapper.selectFxDistributionById(id);
    }

    /**
     * 查询分销提成列表
     * 
     * @param fxDistribution 分销提成
     * @return 分销提成
     */
    @Override
    public List<FxDistribution> selectFxDistributionList(FxDistribution fxDistribution)
    {
        return fxDistributionMapper.selectFxDistributionList(fxDistribution);
    }

    /**
     * 新增分销提成
     * 
     * @param fxDistribution 分销提成
     * @return 结果
     */
    @Override
    public int insertFxDistribution(FxDistribution fxDistribution)
    {
        fxDistribution.setCreateTime(DateUtils.getNowDate());
        return fxDistributionMapper.insertFxDistribution(fxDistribution);
    }

    /**
     * 计算分销提成
     * @param fxOrder
     * @return
     */
    @Override
    public int insertFxDistribution(FxOrder fxOrder) {
        // 订单总金额
        BigDecimal money = new BigDecimal(fxOrder.getPayMoney());
        // 分销比例
        FxDistributionSetting fxDistributionSetting = fxDistributionSettingService.selectFxDistributionRatio();
        boolean ratio = StringUtils.isNotNull(fxDistributionSetting);
        BigDecimal rates = new BigDecimal(ratio ? fxDistributionSetting.getRatio().toString() : "0");
        BigDecimal a = rates.divide(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP);
        // 计算分销金额
        money = money.multiply(a).setScale(2,RoundingMode.HALF_UP);
        // 写入分销表
        FxDistribution fxDistribution = new FxDistribution();
        fxDistribution.setUserId(fxOrder.getUserId());
        fxDistribution.setOrderId(fxOrder.getId());
        fxDistribution.setOrderMoney(new BigDecimal(fxOrder.getPayMoney()).setScale(2, RoundingMode.HALF_UP).toString());
        fxDistribution.setPercentage(money.toString());
        fxDistribution.setCreateTime(DateUtils.getNowDate());
        fxDistribution.setStoreId(fxOrder.getStoreId());
        return fxDistributionMapper.insertFxDistribution(fxDistribution);
    }

    /**
     * 修改分销提成
     * 
     * @param fxDistribution 分销提成
     * @return 结果
     */
    @Override
    public int updateFxDistribution(FxDistribution fxDistribution)
    {
        return fxDistributionMapper.updateFxDistribution(fxDistribution);
    }

    /**
     * 批量删除分销提成
     * 
     * @param ids 需要删除的分销提成主键
     * @return 结果
     */
    @Override
    public int deleteFxDistributionByIds(Long[] ids)
    {
        return fxDistributionMapper.deleteFxDistributionByIds(ids);
    }

    /**
     * 删除分销提成信息
     * 
     * @param id 分销提成主键
     * @return 结果
     */
    @Override
    public int deleteFxDistributionById(Long id)
    {
        return fxDistributionMapper.deleteFxDistributionById(id);
    }

    public void guessTheNumberGame() {
        int number = 10000;
        Random random = new Random();
        int secretNumber = random.nextInt(number) + 1;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Guess the Number game!");
        System.out.println("I'm thinking of a number between 1 and "+ number +".");

        while (true) {
            System.out.println("Enter a guess:");
            int guess = scanner.nextInt();

            if (guess == secretNumber) {
                System.out.println("Congratulations, you guessed it!");
                break;
            } else if (guess > secretNumber) {
                System.out.println("Too high, try again.");
            } else {
                System.out.println("Too low, try again.");
            }
        }

        scanner.close();
    }

    public static void main(String[] args) {
//        FxDistributionServiceImpl fxDistributionService = new FxDistributionServiceImpl();
//        fxDistributionService.guessTheNumberGame();
        JFrame frame = new JFrame("监听窗口");
        JPanel panel = new JPanel();

        // 添加一个标签用于显示按键信息
        JLabel label = new JLabel("按下键");
        panel.add(label);
        frame.add(panel);

        // 添加键盘事件监听器
        panel.setFocusable(true); // 确保面板可以获取焦点
        panel.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // 获取按下的键信息
                int keyCode = e.getKeyCode();
                String keyText = KeyEvent.getKeyText(keyCode);
                label.setText("Key Pressed: " + keyText);
            }
        });

        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public StatisticsVo selectStatistics() {
        Long storeId = null;
        Long userId = SecurityUtils.getUserId();
//        Integer role = sysUserRoleMapper.selectByUserId(userId);
        FxUser fxUser = new FxUser();
        fxUser.setSysUserid(userId);
        List<FxUser> userList = fxUserMapper.selectFxUserList(fxUser);
        if (StringUtils.isNotNull(userList)) {
            FxUser user = userList.get(0);
            if (StringUtils.isNotNull(user.getStoreId())) {
                storeId = user.getStoreId();
            }else {
                return new StatisticsVo();
            }
        }
        return fxDistributionMapper.selectStatistics(storeId);
    }
}
